<template>
  <div>
    <el-dialog :visible.sync="dialogShow" width="80%" :close-on-click-modal="false" :close-on-press-escape="false">
      <div slot="title">
        <span class="font-size-16 font-weight-700 padding-right-20">退款</span>
        <span>可退金额：{{ (currentBill.remainingAmount || 0) | fmoneyThousand }}</span>
      </div>

      <form-common v-if="sendBackFiledList.length > 0" :filed-list="sendBackFiledList" ref="infoForm"></form-common>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogShow = false" class="el-button-reset">取 消</el-button>
        <el-button type="primary" @click="confirmFn">确 认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { sendBackFiledList } from '../../../assets/form';
  import formCommon from '@g/components/form/formCommon.vue';
  import { apiBillInfo, apiDepositDepositReturn } from '../../../api/depositManage';
  import { requestTips } from '@js/utils';

  export default {
    name: 'sendBack',
    props: {
      currentBill: {
        type: Object,
        default: () => {
          return {};
        },
      },
    },

    data() {
      return {
        dialogShow: false,
        sendBackFiledList: [],
      };
    },

    methods: {
      init() {
        this.dialogShow = true;
        this.getBillInfo();
      },

      //账单详情
      async getBillInfo() {
        this.sendBackFiledList = [];
        let res = await apiBillInfo({ billKey: this.currentBill.primaryKey });
        res.clientName = '';
        res.partnerList &&
          res.partnerList.forEach(item => {
            res.clientName = item.partnerName + ',';
          });
        res.clientName = res.clientName.slice(0, res.clientName.length - 1);
        sendBackFiledList.forEach(item => {
          if (item.bindFieldName == 'contractName') {
            item.bindFieldVal = res.contractName;
          }
          if (item.bindFieldName == 'projectName') {
            item.bindFieldVal = res.projectName;
          }
          if (item.bindFieldName == 'clientName') {
            item.bindFieldVal = res.clientName;
          }
        });
        this.sendBackFiledList = sendBackFiledList;
      },

      async confirmFn() {
        if (!this.currentBill.remainingAmount || this.currentBill.remainingAmount == 0) {
          this.$message.warning('暂无可退金额');
          return;
        }
        let model = this.$refs.infoForm.validateForm();
        if (!model) return;
        model.primaryKey = this.currentBill.primaryKey;
        let res = await apiDepositDepositReturn(model);
        requestTips(res, () => {
          this.dialogShow = false;
          this.$emit('refresh');
        });
      },
    },

    mounted() {},

    components: { formCommon },
  };
</script>

<style lang="scss" scoped></style>
